

clear
set more off
 

***Set working directory - end users will want to change this
global workdir "E:\SRTR\HCV\replication kit\"

*** within this working directory, create 5 subfolders named: inputdata, outputdata, code, logs, output
*** load the SRTR data files into the inputdata subfolder once received via SRTR data request

capture log close
log using "$workdir\logs\data construction.log", text replace


*******************************************************************
***SRTR files, create listing datasets with timing of spells (useful for those with multiple listings)
*******************************************************************


use "$workdir\inputdata\cand_liin.dta", clear

drop if wl_org=="IN" // dropping intestine candidates, leaves only liver candidates

sort pers_id can_listing_dt can_rem_dt rec_tx_dt

by pers_id: gen obs_num = _n

duplicates tag pers_id, gen(listing_freq1)

gen listing_freq = listing_freq1 + 1

drop listing_freq1

label define removal 4 "dec donor tx"  5 "med unsuitable"  6 "refused tx"  7 "transfer diff ctr" 8 "died"  9 "other"  10 "listed in error" 11 "listed for unacceptable antigens only" 12 "cond. improv., tx not needed" 13 "too sick" 14 "tx another center (multi-listed)" 15 "living donor tx" 16 "removed in error" 17 "changed to KP" 18 "dec donor emergency tx" 19 "dec donor multi-organ tx" 20 "program inactive for 2+ years" 21 "patient died during tx" 22 "tx another country" 23 "died during liv donor tx" 24 "unable to contact" 

label values can_rem_cd removal 

gen can_rem_cd_num = can_rem_cd

gen neverregistered = 0
replace neverregistered = 1 if don_ty=="L" & missing(can_listing_dt)
replace can_listing_dt = rec_tx_dt if don_ty=="L" & missing(can_listing_dt)

sort pers_id wl_org can_listing_dt can_rem_dt rec_tx_dt

gen rec_tx_dt_new = rec_tx_dt
format  %dD_m_Y  rec_tx_dt_new

*carries previous transplant dates down
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n-1] if missing(rec_tx_dt_new)
replace rec_tx_dt_new = . if can_listing_dt>rec_tx_dt_new

*copies upward transplant dates from later registrations
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)

*additional effort to fill in dates to group by spell
by pers_id: replace rec_tx_dt_new = rec_tx_dt_new[_n-1] if missing(rec_tx_dt_new) & can_listing_dt>=can_listing_dt[_n-1] & can_listing_dt<=rec_tx_dt_new[_n-1] & can_rem_dt<rec_tx_dt_new & !missing(rec_tx_dt_new[_n-1])

*generating an end of spell date to include spells that end for reasons other than transplantation
gen spell_end_dt = rec_tx_dt_new
format  %dD_m_Y  spell_end_dt

gen min_death_dt = min(pers_ssa_death_dt,pers_optn_death_dt)
format  %dD_m_Y  min_death_dt

gen alt_death_dt = .
replace alt_death_dt = can_rem_dt if can_rem_cd==8
by pers_id wl_org: egen alt_min_death_dt = min(alt_death_dt) 

*spell end date is death date for those who died on last spell
replace spell_end_dt = min_death_dt if can_rem_cd==8 & missing(spell_end_dt)
replace spell_end_dt = alt_min_death_dt if can_rem_cd==8 & missing(spell_end_dt) & missing(min_death_dt)

*carries previous spell end dates down
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n-1] if missing(spell_end_dt)
replace spell_end_dt = . if can_listing_dt>spell_end_dt

*copying upward end dates 
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)

*replacing end date as latest removal date among listings that didn't have a well-defined ending (death or tx)
by pers_id wl_org: egen maxmissingcanremdt = max(can_rem_dt) if missing(spell_end_dt)
format  %dD_m_Y  maxmissingcanremdt

*replacing end date as latest listing date among listings that haven't ended yet
by pers_id wl_org: egen maxcanlistdt = max(can_listing_dt) if missing(spell_end_dt)
format  %dD_m_Y  maxcanlistdt

replace spell_end_dt = max(maxmissingcanremdt,maxcanlistdt) if missing(spell_end_dt)

replace spell_end_dt = maxcanlistdt if missing(spell_end_dt) & missing(can_rem_dt)

order pers_id px_id wl_org  can_listing_dt can_rem_dt rec_tx_dt rec_tx_dt_new spell_end_dt can_prev_tx can_rem_cd can_rem_cd_num obs_num listing_freq
sort pers_id wl_org spell_end_dt can_listing_dt can_rem_dt

gen misscanremdt = 0
replace misscanremdt = 1 if missing(can_rem_dt)
by pers_id wl_org spell_end_dt: egen summisscanremdt = sum(misscanremdt)
*last date of data, give it to spells that have at least one open registration
replace spell_end_dt = 22706 if summisscanremdt>0 & !missing(summisscanremdt) & spell_end_dt==max(maxmissingcanremdt,maxcanlistdt) // end date based on March 2022 extract (22706 = March 2, 2022)

sort pers_id wl_org spell_end_dt can_listing_dt can_rem_dt

by pers_id wl_org spell_end_dt: gen spelllistingindex = _n
by pers_id wl_org spell_end_dt: egen listingsinspell = max(spelllistingindex)

gen can_rem_month = month(can_rem_dt)
gen can_rem_year = year(can_rem_dt)
gen can_rem_ym = ym(can_rem_year,can_rem_month)

gen can_listing_month = month(can_listing_dt)
gen can_listing_year = year(can_listing_dt)
gen can_listing_ym = ym(can_listing_year,can_listing_month)

gen spell_end_month = month(spell_end_dt)
gen spell_end_year = year(spell_end_dt)
gen spell_end_ym = ym(spell_end_year,spell_end_month)

gen year = can_listing_year
gen ym = can_listing_ym

saveold "$workdir\outputdata\liver listings.dta", replace

*****************************************************
***Do same thing for kidneys
*****************************************************

use "$workdir\inputdata\cand_kipa.dta", clear

drop if wl_org!="KI" // dropping intestine candidates, leaves only liver candidates

sort pers_id can_listing_dt can_rem_dt rec_tx_dt

by pers_id: gen obs_num = _n

duplicates tag pers_id, gen(listing_freq1)

gen listing_freq = listing_freq1 + 1

drop listing_freq1

label define removal 4 "dec donor tx"  5 "med unsuitable"  6 "refused tx"  7 "transfer diff ctr" 8 "died"  9 "other"  10 "listed in error" 11 "listed for unacceptable antigens only" 12 "cond. improv., tx not needed" 13 "too sick" 14 "tx another center (multi-listed)" 15 "living donor tx" 16 "removed in error" 17 "changed to KP" 18 "dec donor emergency tx" 19 "dec donor multi-organ tx" 20 "program inactive for 2+ years" 21 "patient died during tx" 22 "tx another country" 23 "died during liv donor tx" 24 "unable to contact" 

label values can_rem_cd removal 

gen can_rem_cd_num = can_rem_cd

gen neverregistered = 0
replace neverregistered = 1 if don_ty=="L" & missing(can_listing_dt)
replace can_listing_dt = rec_tx_dt if don_ty=="L" & missing(can_listing_dt)

sort pers_id wl_org can_listing_dt can_rem_dt rec_tx_dt

gen rec_tx_dt_new = rec_tx_dt
format  %dD_m_Y  rec_tx_dt_new

*carries previous transplant dates down
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n-1] if missing(rec_tx_dt_new)
replace rec_tx_dt_new = . if can_listing_dt>rec_tx_dt_new

*copies upward transplant dates from later registrations
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)
by pers_id wl_org: replace rec_tx_dt_new = rec_tx_dt_new[_n+1] if missing(rec_tx_dt_new)

*additional effort to fill in dates to group by spell
by pers_id: replace rec_tx_dt_new = rec_tx_dt_new[_n-1] if missing(rec_tx_dt_new) & can_listing_dt>=can_listing_dt[_n-1] & can_listing_dt<=rec_tx_dt_new[_n-1] & can_rem_dt<rec_tx_dt_new & !missing(rec_tx_dt_new[_n-1])

*generating an end of spell date to include spells that end for reasons other than transplantation
gen spell_end_dt = rec_tx_dt_new
format  %dD_m_Y  spell_end_dt

gen min_death_dt = min(pers_ssa_death_dt,pers_optn_death_dt)
format  %dD_m_Y  min_death_dt

gen alt_death_dt = .
replace alt_death_dt = can_rem_dt if can_rem_cd==8
by pers_id wl_org: egen alt_min_death_dt = min(alt_death_dt) 

*spell end date is death date for those who died on last spell
replace spell_end_dt = min_death_dt if can_rem_cd==8 & missing(spell_end_dt)
replace spell_end_dt = alt_min_death_dt if can_rem_cd==8 & missing(spell_end_dt) & missing(min_death_dt)

*carries previous spell end dates down
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n-1] if missing(spell_end_dt)
replace spell_end_dt = . if can_listing_dt>spell_end_dt

*copying upward end dates 
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)
by pers_id wl_org: replace spell_end_dt = spell_end_dt[_n+1] if missing(spell_end_dt)

*replacing end date as latest removal date among listings that didn't have a well-defined ending (death or tx)
by pers_id wl_org: egen maxmissingcanremdt = max(can_rem_dt) if missing(spell_end_dt)
format  %dD_m_Y  maxmissingcanremdt

*replacing end date as latest listing date among listings that haven't ended yet
by pers_id wl_org: egen maxcanlistdt = max(can_listing_dt) if missing(spell_end_dt)
format  %dD_m_Y  maxcanlistdt

replace spell_end_dt = max(maxmissingcanremdt,maxcanlistdt) if missing(spell_end_dt)

replace spell_end_dt = maxcanlistdt if missing(spell_end_dt) & missing(can_rem_dt)

order pers_id px_id wl_org  can_listing_dt can_rem_dt rec_tx_dt rec_tx_dt_new spell_end_dt can_prev_tx can_rem_cd can_rem_cd_num obs_num listing_freq
sort pers_id wl_org spell_end_dt can_listing_dt can_rem_dt

gen misscanremdt = 0
replace misscanremdt = 1 if missing(can_rem_dt)
by pers_id wl_org spell_end_dt: egen summisscanremdt = sum(misscanremdt)
*last date of data, give it to spells that have at least one open registration
replace spell_end_dt = 22706 if summisscanremdt>0 & !missing(summisscanremdt) & spell_end_dt==max(maxmissingcanremdt,maxcanlistdt) // end date based on March 2022 extract (22706 = March 2, 2022)

sort pers_id wl_org spell_end_dt can_listing_dt can_rem_dt

by pers_id wl_org spell_end_dt: gen spelllistingindex = _n
by pers_id wl_org spell_end_dt: egen listingsinspell = max(spelllistingindex)

gen can_rem_month = month(can_rem_dt)
gen can_rem_year = year(can_rem_dt)
gen can_rem_ym = ym(can_rem_year,can_rem_month)

gen can_listing_month = month(can_listing_dt)
gen can_listing_year = year(can_listing_dt)
gen can_listing_ym = ym(can_listing_year,can_listing_month)

gen spell_end_month = month(spell_end_dt)
gen spell_end_year = year(spell_end_dt)
gen spell_end_ym = ym(spell_end_year,spell_end_month)

gen year = can_listing_year
gen ym = can_listing_ym

saveold "$workdir\outputdata\kidney listings.dta", replace


***********************************************************************************************************************************
** Additional data prep for files to merge on below

use "$workdir\inputdata\opo_to_tx_hospital.dta", clear

sort txc_ctr_cd txc_ctr_ty start_dt end_dt

by txc_ctr_cd txc_ctr_ty: gen index = _n
by txc_ctr_cd txc_ctr_ty: egen maxindex = max(index)
keep if index==maxindex
drop index maxindex

saveold "$workdir\outputdata\opo_to_tx_hospital_for_merge.dta", replace


***********************************************************************************************************************************

use "$workdir\outputdata\liver listings.dta", clear
append using "$workdir\outputdata\kidney listings.dta"

gen ctr_id = can_listing_ctr_id
merge m:1 ctr_id using "$workdir\inputdata\institution.dta", gen(_merge)
drop if _merge==2
drop _merge
drop ctr_id

// this procedure effectively combines CTHH (92) and MAOB (302) for the listing OPO ID.  The merge happened in 2021-22, and the opo to tx hospital file was created in 2022 and provided to me, which codes CTHH as part of MAOB
rename ctr_cd txc_ctr_cd
rename ctr_ty txc_ctr_ty
merge m:1 txc_ctr_cd txc_ctr_ty using "$workdir\outputdata\opo_to_tx_hospital_for_merge.dta", gen(_merge) keepusing(served_opo_cd served_opo_ty)
replace served_opo_cd = txc_ctr_cd if txc_ctr_cd=="MAOB" // this is the only txc_ctr_cd that doesn't have an OPO code in the opo to TX hospital crosswalk
replace served_opo_ty = txc_ctr_ty if txc_ctr_cd=="MAOB" 
drop if _merge==2
drop _merge

gen ctr_cd = served_opo_cd
gen ctr_ty = served_opo_ty
merge m:1 ctr_cd ctr_ty using "$workdir\inputdata\institution.dta", gen(_merge) keepusing(ctr_id)
drop if _merge==2
drop _merge
rename ctr_id can_listing_opo_id
drop ctr_cd ctr_ty

recode can_listing_opo_id (741 = 832) (766 = 92) (129 = 138) (144 = 143) (163 = 169) (219 223 228 = 222) (451 = 450) (473 = 471) (490 = 494) (623 = 660) (649 = 637) (731 732 = 730) (751 = 530) (57 = 52)  (704=706)


*demographic characteristics

gen medicaid = 0
replace medicaid = . if missing(can_primary_pay) & missing(can_secondary_pay)
replace medicaid = . if can_primary_pay==15 & missing(can_secondary_pay) // 15 = "unknown"
replace medicaid = . if can_primary_pay==15 & can_secondary_pay==10 // secondary = 10 is "none"
replace medicaid = 1 if can_primary_pay==2 | can_secondary_pay==2

gen educ_lteqhs = 0
replace educ_lteqhs = . if missing(can_education)
replace educ_lteqhs = . if inlist(can_education,996,998)
replace educ_lteqhs = 1 if inlist(can_education,1,2,3) // none, grade school, HS/GED (4 is attended coll/tech school, 5 is AA or BA, 6 is grad degree)


**to avoid lumping in minors, who are subject to different allocation rules and insurance coverage policies
merge 1:1 px_id using "$workdir\inputdata\tx_li.dta", keepusing(rec_age_at_tx) 
drop if _merge==2
drop _merge
rename rec_age_at_tx rec_age_at_tx_li

merge 1:1 px_id using "$workdir\inputdata\tx_ki.dta", keepusing(rec_age_at_tx) 
drop if _merge==2
drop _merge
rename rec_age_at_tx rec_age_at_tx_ki

drop if rec_age_at_tx_ki<18 & missing(can_age_at_listing) //to avoid lumping in minors, who are subject to different allocation rules and insurance coverage policies
drop if rec_age_at_tx_li<18 & missing(can_age_at_listing) //to avoid lumping in minors, who are subject to different allocation rules and insurance coverage policies
drop if can_age_at_listing<18 //to avoid lumping in minors, who are subject to different allocation rules and insurance coverage policies


gen toosickdied = .
replace toosickdied = 0 if !missing(can_rem_cd)
replace toosickdied = 1 if inlist(can_rem_cd,8,13)

gen improved = .
replace improved = 0 if !missing(can_rem_cd)
replace improved = 1 if inlist(can_rem_cd,12)

gen ddtx = 0
replace ddtx = 1 if don_ty=="C"

gen ldtx = 0
replace ldtx = 1 if don_ty=="L"

gen tx = 0
replace tx = 1 if !missing(don_ty)

saveold "$workdir\outputdata\listings with merges.dta", replace


******************************************
*** create time-varying MELD scores for future merge

use "$workdir\inputdata\stathist_liin.dta", clear

sort px_id wl_org canhx_begin_dt

keep if wl_org=="LI"

gen wl_year = year(canhx_begin_dt)
gen wl_month = month(canhx_begin_dt)
gen wl_ym = ym(wl_year,wl_month)

gen status_meld = canhx_stat_cd-6200
replace status_meld = . if !inrange(canhx_stat_cd,6188,6299)

gen lab_meld = canhx_srtr_lab_meld-6200


collapse (lastnm) status_meld lab_meld , by(px_id wl_ym)


saveold "$workdir\outputdata\MELD composition of WL.dta", replace


*************************************************************
*** Creating WL composition datasets

use "$workdir\outputdata\listings with merges.dta", clear

***
//drop if wl_org=="KI"
drop if missing(can_age_at_listing) // 262 of these, got a living liver transplant without registering on WL, probably roughly 40k for kidneys

keep px_id pers_id wl_org can_listing_dt can_listing_ym can_listing_year can_rem_dt can_rem_ym can_rem_year rec_tx_dt spell_end_ym ///
can_dgn can_dgn2 can_dgn_ostxt can_primary_pay can_secondary_pay can_race_srtr can_gender ///
can_init_stat can_init_srtr_lab_meld can_init_act_stat_cd can_acpt_hcv_pos toosickdied improved ddtx ldtx tx can_listing_opo_id served_opo_cd

gen monthsonwl = can_rem_ym - can_listing_ym
replace monthsonwl = spell_end_ym - can_listing_ym if missing(monthsonwl)

expand monthsonwl+1

bysort pers_id px_id wl_org: gen index = _n

gen wl_ym = can_listing_ym - 1 + index
gen date = dofm(wl_ym)
gen wl_year = year(date)
drop date

*Merge on MELD score over time here
merge 1:1 px_id wl_ym using "$workdir\outputdata\MELD composition of WL.dta", keepusing(status_meld lab_meld)
drop if _merge==2

replace status_meld = can_init_stat-6200 if inrange(can_init_stat,6206,6250)

bysort px_id: replace status_meld = status_meld[_n-1] if missing(status_meld)
bysort px_id: replace lab_meld = lab_meld[_n-1] if missing(lab_meld)

gen one = 1

gen can_dgn_hep = .
replace can_dgn_hep = 0 if inlist(can_dgn,4100,4101,4102,4108,4110,4200,4202,4208,4210,4212,4213,4214,4215,4217,4220,4230)
replace can_dgn_hep = 0 if inlist(can_dgn,4235,4240,4241,4242,4245,4260,4270,4272,4275,4280,4285,4290,4300,4301,4302,4303)
replace can_dgn_hep = 0 if inlist(can_dgn,4307,4308,4315,4403,4405,4410,4420,4430,4450,4451,4455,4500)
replace can_dgn_hep = 1 if inlist(can_dgn,4204,4216,4104,4106,4206,4593)
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HCV")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIS C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEP C") 
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATIS C") 
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIC C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEP. C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIS BC")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPC")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEP-C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIS B AND C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITS C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPITITS C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPITITIS C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEP A/C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIS A/C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITUS C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATIC FAILURE - VIRAL C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEP B & C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIS-C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "TYPE C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "TYPE B AND C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "TYPE B & C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEIP B/C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HVC")

gen can_dgn_nothep = 0 if !missing(can_dgn_hep)
replace can_dgn_nothep = 1 if can_dgn_hep==0

gen lab_meld_hcv = lab_meld if can_dgn_hep==1
gen lab_meld_nonhcv = lab_meld if can_dgn_hep==0

preserve

/*
***DSA recoding - need to consider this
*115, OurLegacy, Florida, starts in 2007
*38, Sierra Donor, California, seems to end liver program in 2008/2009, should be straightforward to recode as 34 for CADN
*369, Mississippi Organ Recovery Agency, starts in 2013
*589 Lifelink Puerto Rico, starts in 2012
*/
gen orig_can_listing_opo_id = can_listing_opo_id // preserve original coding
replace can_listing_opo_id = 34 if orig_can_listing_opo_id==38 // Sierra Donor, CA -> CADN (map shows that Sierra is surrounded by the CADN region)
replace can_listing_opo_id = 998 if inlist(orig_can_listing_opo_id,369,222,5) // combine new MS liver program with existing TN/MS program, LA program, and AL program
replace can_listing_opo_id = 999 if substr(served_opo_cd,1,2)=="FL" // combine all FL OPOs to roll in the new FL program that started in 2007, and FLSW which supposedly ended in 2004
replace can_listing_opo_id = 999 if orig_can_listing_opo_id==589 // include PR with FL
replace can_listing_opo_id = 302 if orig_can_listing_opo_id==92 // recoding CTHH to be part of MAOB -- this seems to have already been done in the candidate files so unnecessary here

collapse (sum) numonwl=one numonwlhcv=can_dgn_hep numonwlnonhcv=can_dgn_nothep (mean) lab_meld lab_meld_hcv lab_meld_nonhcv, by(can_listing_opo_id wl_year wl_org)

fillin can_listing_opo_id wl_year wl_org

foreach x in numonwl numonwlhcv numonwlnonhcv {
	replace `x' = 0 if missing(`x')
}

foreach x in numonwl numonwlhcv numonwlnonhcv {
	replace `x' = `x'/12
}

saveold "$workdir\outputdata\WL composition by OPO and year.dta", replace

restore

/*
***DSA recoding - need to consider this
*115, OurLegacy, Florida, starts in 2007
*38, Sierra Donor, California, seems to end liver program in 2008/2009, should be straightforward to recode as 34 for CADN
*369, Mississippi Organ Recovery Agency, starts in 2013
*589 Lifelink Puerto Rico, starts in 2012
*/
gen orig_can_listing_opo_id = can_listing_opo_id // preserve original coding
replace can_listing_opo_id = 34 if orig_can_listing_opo_id==38 // Sierra Donor, CA -> CADN (map shows that Sierra is surrounded by the CADN region)
replace can_listing_opo_id = 998 if inlist(orig_can_listing_opo_id,369,222,5) // combine new MS liver program with existing TN/MS program, LA program, and AL program
replace can_listing_opo_id = 999 if substr(served_opo_cd,1,2)=="FL" // combine all FL OPOs to roll in the new FL program that started in 2007, and FLSW which supposedly ended in 2004
replace can_listing_opo_id = 999 if orig_can_listing_opo_id==589 // include PR with FL
replace can_listing_opo_id = 302 if orig_can_listing_opo_id==92 // recoding CTHH to be part of MAOB -- this seems to have already been done in the candidate files so unnecessary here

collapse (sum) numonwl=one numonwlhcv=can_dgn_hep numonwlnonhcv=can_dgn_nothep (mean) lab_meld lab_meld_hcv lab_meld_nonhcv, by(wl_ym wl_org)

fillin wl_ym wl_org

foreach x in numonwl numonwlhcv numonwlnonhcv {
	replace `x' = 0 if missing(`x')
}

saveold "$workdir\outputdata\WL composition.dta", replace


***********************************************************************
** Data for recovery/discard/usage analyses

use "$workdir\inputdata\donor_disposition.dta", clear

gen month = month(don_recov_dt)
gen year = year(don_recov_dt)
gen ym = ym(year,month)
format ym %tm

encode don_org, gen(organ)
* drop label organ

sort donor_id

tab don_disposition
tab  don_discard_cd don_disposition
tab don_reason_cd don_disposition

***Most are single organs, but some are double - count each one separately?
* Variable "organ" = 1 for "double lung" and = 2 for "en bloc kidney", meaning "double kidney"
gen numorg = 1
replace numorg = 2 if organ == 1|organ==2
tab organ numorg

***Aggregate all lungs into 1 category
gen organagg = 1 if organ == 1|organ==11|organ==12
replace organagg = 2 if organ == 2|organ==10|organ==16
replace organagg = 3 if organ == 3
replace organagg = 4 if organ == 4|organ==5|organ==6
replace organagg = 5 if organ == 7|organ==8|organ==9
replace organagg = 6 if organ == 13|organ==14|organ==15

label define o 1 "lung" 2 "kidney" 3 "heart" 4 "intestine" 5 "liver" 6 "pancreas"

label values organagg o

merge m:1 donor_id using "$workdir\inputdata\donor_deceased.dta"
drop _merge

gen ctr_id = don_opo_ctr_id
merge m:1 ctr_id using "$workdir\inputdata\institution.dta", gen(_merge2) keepusing(ctr_cd)
drop if _merge2==2
drop _merge2

recode don_opo_ctr_id (741 = 832) (766 = 92) (129 = 138) (144 = 143) (163 = 169) (219 223 228 = 222) (451 = 450) (473 = 471) (490 = 494) (623 = 660) (649 = 637) (731 732 = 730) (751 = 530) (57 = 52)  (704=706)

/*
***DSA recoding - need to consider this
*115, OurLegacy, Florida, starts in 2007
*38, Sierra Donor, California, seems to end liver program in 2008/2009, should be straightforward to recode as 34 for CADN
*369, Mississippi Organ Recovery Agency, starts in 2013
*589 Lifelink Puerto Rico, starts in 2012
*/
gen orig_don_opo_ctr_id = don_opo_ctr_id // preserve original coding
replace don_opo_ctr_id = 34 if orig_don_opo_ctr_id==38 // Sierra Donor, CA -> CADN (map shows that Sierra is surrounded by the CADN region)
replace don_opo_ctr_id = 998 if inlist(orig_don_opo_ctr_id,369,222,5) // combine new MS liver program with existing TN/MS program, LA program, and AL program
replace don_opo_ctr_id = 999 if substr(ctr_cd,1,2)=="FL" // combine all FL OPOs to roll in the new FL program that started in 2007, and FLSW which supposedly ended in 2004
replace don_opo_ctr_id = 999 if orig_don_opo_ctr_id==589 // include PR with FL
replace don_opo_ctr_id = 302 if orig_don_opo_ctr_id==92 // recoding CTHH to be part of MAOB (to match the candidate files)

keep if inrange(year,2005,2019)

keep if inlist(organagg,2,5)

gen all = 1

gen recovered = don_disposition == 4|don_disposition == 5|don_disposition == 6

gen transplanted = don_disposition == 6

gen discarded_norecip = .
replace discarded_norecip = 1 if don_discard_cd==620 // 620 is "no recipient located - list exhausted"
replace discarded_norecip = 1 if don_disposition==3 & inlist(don_reason_cd,208,215,216,217) // 208, 215, 216, 217 refer to no recipient located or organ refused by all among "organ not recovered" disposition

label variable discarded "organ not recovered due to no recipient/refusal, or recovered but not TX bc no recipient could be located"

gen poorqual = . 
replace poorqual = 1 if don_disposition==1 & inlist(don_reason_cd,18,21) // renal failure, donor quality
replace poorqual = 1 if don_disposition==3 & inlist(don_reason_cd,200,202,203,204,205,206,207,209,210,211,212,218,219,223,224) // issues of poor quality of donor
replace poorqual = 1 if inrange(don_discard_cd,601,619) // recovered for tx but not tx because of quality reasons

label variable poorqual "consent not requested or organ not recovered - all reason codes with poor quality"

gen poorqual_recov = . 
replace poorqual_recov = 1 if inrange(don_discard_cd,601,619) // recovered for tx but not tx because of quality reasons

gen poorqual_notrecov = . 
replace poorqual_notrecov = 1 if don_disposition==1 & inlist(don_reason_cd,18,21) // renal failure, donor quality
replace poorqual_notrecov = 1 if don_disposition==3 & inlist(don_reason_cd,200,202,203,204,205,206,207,209,210,211,212,218,219,223,224) // issues of poor quality of donor

gen discarded = .
replace discarded = 1 if discarded_norecip==1 | poorqual==1

gen reason_hepatitis = .
replace reason_hepatitis = 1 if don_discard_cd==610 // positive hepatitis
replace reason_hepatitis = 1 if don_reason_cd==203 // positive hepatitis

gen hcv_donor = .
replace hcv_donor = 0 if don_anti_hcv=="N"
replace hcv_donor = 1 if don_anti_hcv=="P"

foreach x in all recovered transplanted discarded discarded_norecip poorqual poorqual_recov poorqual_notrecov reason_hepatitis {
	replace `x' = 0.5 if inlist(don_org,"LIS1","LIS2") & `x'==1
	replace `x' = 2 if inlist(don_org,"EKI") & `x'==1
}

foreach x in all recovered transplanted discarded discarded_norecip poorqual poorqual_recov poorqual_notrecov reason_hepatitis {
	gen `x'_hcv = `x'*hcv_donor
}

collapse (sum) all recovered transplanted discarded discarded_norecip poorqual poorqual_recov poorqual_notrecov reason_hepatitis all_hcv recovered_hcv transplanted_hcv discarded_hcv discarded_norecip_hcv poorqual_hcv poorqual_recov_hcv poorqual_notrecov_hcv reason_hepatitis_hcv, by(organagg don_opo_ctr_id year)

//label define o 1 "lung" 2 "kidney" 3 "heart" 4 "intestine" 5 "liver" 6 "pancreas"
label values organagg o


saveold "$workdir\outputdata\donor disposition sample.dta", replace


*************************************************************
*** Main Estimation Sample Construction

use "$workdir\outputdata\listings with merges.dta", clear


merge 1:1 px_id using "$workdir\inputdata\tx_li.dta", keepusing(rec_hcv_stat) gen(_mergetxdatali)
drop if _mergetxdatali==2
rename rec_hcv_stat rec_hcv_stat_li

merge 1:1 px_id using "$workdir\inputdata\tx_ki.dta", keepusing(rec_hcv_stat) gen(_mergetxdataki)
drop if _mergetxdataki==2
rename rec_hcv_stat rec_hcv_stat_ki


tab can_dgn rec_hcv_stat_li if inlist(rec_hcv_stat_li,"P","N"), col // tabulates primary diagnosis by hcv status
tab can_dgn2 rec_hcv_stat_li if inlist(rec_hcv_stat_li,"P","N"), col // tabulates secondary diagnosis by hcv status (see results below)

tab can_dgn rec_hcv_stat_ki if inlist(rec_hcv_stat_ki,"P","N"), col // tabulates primary diagnosis by hcv status
tab can_dgn2 rec_hcv_stat_ki if inlist(rec_hcv_stat_ki,"P","N"), col // tabulates secondary diagnosis by hcv status (see results below)

** Note: no obvious HCV-attributable diagnosis codes for KI other than maybe:
*3048: KI:CALCINEURIN INHIBITOR NEPHROTOXICITY (464 patients, or 2.19% of all HCV+ KI TX recipients, vs. 0.65% of HCV- KI recipients)
*3049: KI:HEROIN NEPHROTOXICITY (39 patients, 0.18% of all HCV+ KI TX recipients vs. 0.02% of HCV- KI TX recipients)
*3072: KI:HEPATORENAL SYNDROME (518 patients, 2.44% of all HCV+ KI TX recipients vs. 0.59% of HCV- KI TX recipients)
*Overall, only 5.3% of KI recipients are HCV+ vs. 35.9% of LI recipients

gen can_dgn_hep = .
replace can_dgn_hep = 0 if inlist(can_dgn,4100,4101,4102,4108,4110,4200,4202,4208,4210,4212,4213,4214,4215,4217,4220,4230)
replace can_dgn_hep = 0 if inlist(can_dgn,4235,4240,4241,4242,4245,4260,4270,4272,4275,4280,4285,4290,4300,4301,4302,4303)
replace can_dgn_hep = 0 if inlist(can_dgn,4307,4308,4315,4403,4405,4410,4420,4430,4450,4451,4455,4500)
replace can_dgn_hep = 1 if inlist(can_dgn,4204,4216,4104,4106,4206,4593)
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HCV")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIS C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEP C") 
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATIS C") 
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIC C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEP. C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIS BC")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPC")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEP-C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIS B AND C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITS C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPITITS C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPITITIS C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEP A/C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIS A/C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITUS C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATIC FAILURE - VIRAL C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEP B & C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEPATITIS-C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "TYPE C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "TYPE B AND C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "TYPE B & C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HEIP B/C")
replace can_dgn_hep = 1 if strpos(can_dgn_ostxt, "HVC")

/*
4204: LI:CIRRHOSIS: TYPE C
4216: LI:ALCOHOLIC CIRRHOSIS WITH HEPATITIS C
4104: LI:AHN: TYPE C
4106: LI:AHN: TYPE B AND C
4206: LI:CIRRHOSIS: TYPE B AND C
4593: LI:Hepatitis C: Chronic or Acute 
*/

gen insurance = ""
replace insurance = "Private" if can_primary_pay==1 
replace insurance = "Medicare FFS" if can_primary_pay==3 
replace insurance = "Medicare Choice" if can_primary_pay==4 
replace insurance = "Medicare Unspec" if can_primary_pay==14 
replace insurance = "Medicaid" if can_primary_pay==2

gen ins_alt = ""
replace ins_alt = "Private" if can_primary_pay==1 
replace ins_alt = "Medicare" if inlist(can_primary_pay,3,4,13)
replace ins_alt = "Medicaid" if can_primary_pay==2 

gen can_dgn_nothep = 1 if can_dgn_hep==0

gen initial_meld = can_init_srtr_lab_meld - 6200

gen last_meld = can_last_srtr_lab_meld - 6200

gen highmeld = 0
replace highmeld = 1 if inrange(initial_meld,32,40)

gen lowmeld = 0
replace lowmeld = 1 if inrange(initial_meld,6,15)

gen middlemeld = 0
replace middlemeld = 1 if inrange(initial_meld,16,31)

gen status1a = 0
replace status1a = 1 if can_init_act_stat_cd==6011

gen acpthcvdon = 0 if !missing(can_acpt_hcv_pos)
replace acpthcvdon = 1 if can_acpt_hcv_pos=="Y"

*** to merge on living donor information
merge m:1 donor_id using "$workdir\inputdata\donor_live.dta" 
drop if _merge==2
rename _merge _mergelivdon

*** to merge on deceased donor information
merge m:1 donor_id using "$workdir\inputdata\donor_deceased.dta" 
drop if _merge==2
rename _merge _mergedecdon

*don_anti_hcv and don_hcv_antibody tests whether you were ever infected with HCV
gen hcv_donor = .
replace hcv_donor = 0 if don_anti_hcv=="N" | don_hcv_antibody=="N"
replace hcv_donor = 1 if don_anti_hcv=="P" | don_hcv_antibody=="P"

gen non_hcv_donor = .
replace non_hcv_donor = 0 if don_anti_hcv=="P" | don_hcv_antibody=="P"
replace non_hcv_donor = 1 if don_anti_hcv=="N" | don_hcv_antibody=="N"

foreach x in toosickdied improved ddtx ldtx tx {
	gen last_meld_`x' = last_meld if `x'==1
}

/*
***DSA recoding - need to consider this
*115, OurLegacy, Florida, starts in 2007
*38, Sierra Donor, California, seems to end liver program in 2008/2009, should be straightforward to recode as 34 for CADN
*369, Mississippi Organ Recovery Agency, starts in 2013
*589 Lifelink Puerto Rico, starts in 2012
*/
gen orig_can_listing_opo_id = can_listing_opo_id // preserve original coding
replace can_listing_opo_id = 34 if orig_can_listing_opo_id==38 // Sierra Donor, CA -> CADN (map shows that Sierra is surrounded by the CADN region)
replace can_listing_opo_id = 998 if inlist(orig_can_listing_opo_id,369,222,5) // combine new MS liver program with existing TN/MS program, LA program, and AL program
replace can_listing_opo_id = 999 if substr(served_opo_cd,1,2)=="FL" // combine all FL OPOs to roll in the new FL program that started in 2007, and FLSW which supposedly ended in 2004
replace can_listing_opo_id = 999 if orig_can_listing_opo_id==589 // include PR with FL
replace can_listing_opo_id = 302 if orig_can_listing_opo_id==92 // Combining CTHH with MAOB to match up with everything else

gen opo_state = substr(served_opo_cd,1,2)

saveold "$workdir\outputdata\estimation sample.dta", replace

	

/*
** Primary codes:

Seem to overwhelmingly indicate HCV: 4204 (59% vs. 2.2%), 4216 (13.3% vs. 0.6%), 4104 (2.9% vs. 0.1%), 4106 (0.1% vs. 0.01%), 4206 (0.9% vs. 0.08%), 4593 (0.03% vs. 0%)

(probably just omit these?) Less clear but still leans toward HCV: 4401 (8% vs 3.5%), 4400 (5% vs. 2.7%), 4510 (0.07% vs. 0.04%)

Seem to overwhelmingly indicate NOT HCV: 4100 (0.9% vs. 0.04%), 4101 (0.1% vs. 0%), 4102 (0.8% vs. 0.1%), 4108 (1.2% vs. 0.1%), 4110 (1.8% vs. 0.2%), 
4200 (0.2% vs. 0.02%), 4202 (3.5% vs. 0.5%), 4208 (3.6% vs. 0.3%), 4210 (1% vs. 0.16%), 4212 (4.3% vs. 0.3%), 4213 (5.6% vs. 0.3%), 4214 (14.3% vs. 0.6%),
4215 (26.7% vs. 3.5%), 4217 (1% vs. 0.08%), 4220 (5.2% vs. 0.2%), 4230 (0.12% vs. 0.01%), 4235 (0.26% vs. 0.05%), 4240 (1.2% vs. 0.07%), 4241 (3.7% vs. 0.1%),
4242 (2.4% vs. 0.1%), 4245 (0.6% vs. 0.05%), 4260 (0.25% vs. 0.06%), 4270 (0.2% vs 0.01%), 4272 (0.06% vs. 0%), 4275 (0.11% vs. 0.01%), 4280 (0.13% vs. 0.02%),
4285 (0.13% vs. 0.02%), 4290 (0.5% vs. 0.02%), 4300 (1.5% vs. 0.07%), 4301 (0.54% vs. 0.04%), 4302 (0.65% vs. 0.1%), 4303 (0.06% vs 0%), 4307 (0.14% vs. 0.01%),
4308 (0.04% vs. 0%), 4315 (0.21% vs. 0.03%), 4403 (0.56% vs. 0.07%), 4405 (0.12% vs. 0.01%), 4410 (0.12% vs. 0.02%), 4420 (0.08% vs. 0%), 4430 (0.1% vs 0.01%),
4450 (0.06% vs. 0.01%), 4451 (0.75% vs. 0.03%), 4455 (0.05% vs. 0%), 4500 (0.13% vs. 0%)

** Secondary codes: see below table
*/

/*

** Overall highest frequency primary codes: 4204 (22.8%), 4215 (19.1%), 4214 (8.6%), 999 (5.7%), 4216 (5.3%), 4401 (4.4%), 4213 (3.7%), 4220 (3.4%), 4400 (3%), 4212 (3%), 4208 (2.9%), 4202 (2.5%), 4241 (2.2%)
** Overall highest frequency secondary codes: 4400 (18.2%), 4204 (16%), 4401 (15.3%), 4215 (15.1%), 999 (8.9%), 4214 (7.3%), ...

** Highest frequency primary codes in order for HCV+ are 4204 (59%), 4216 (13%), 4401 (8%), 4400 (5%), 4215 (3.5%), 999 (3%), 4104 (3%)
** Highest frequency primary codes in order for HCV- are 4215 (26.7%), 4214 (14.3%), 999 (5.6%), 4213 (5.6%), 4220 (5.2%), 4212 (4.3%), 4241 (3.7%), 4208 (3.6%), 4202 (3.5%), 4401 (3.5%), 4400 (2.7%)

** Highest frequency secondary codes in order for HCV+ are 4204 (27%), 4400 (23%), 4401 (19.7%), 4215 (13%), 999 (5.2%), 4216 (4.6%)
** Highest frequency secondary codes in order for HCV- are 4400 (16%), 4215 (15.1%), 4214 (14.7%), 4401 (12.3%), 999 (10.2%), 4202 (4.4%), 4212 (4.2%)

** PRIMARY DGN

. tab can_dgn rec_hcv_stat if inlist(rec_hcv_stat,"P","N"), col

+-------------------+
| Key               |
|-------------------|
|     frequency     |
| column percentage |
+-------------------+

   Primary |  HCV serology status
 Diagnosis |         N          P |     Total
-----------+----------------------+----------
       999 |     5,254      1,643 |     6,897 
           |      5.62       3.14 |      4.73 
-----------+----------------------+----------
      4100 |       825         23 |       848 
           |      0.88       0.04 |      0.58 
-----------+----------------------+----------
      4101 |       103          0 |       103 
           |      0.11       0.00 |      0.07 
-----------+----------------------+----------
      4102 |       774         47 |       821 
           |      0.83       0.09 |      0.56 
-----------+----------------------+----------
      4104 |       128      1,502 |     1,630 
           |      0.14       2.87 |      1.12 
-----------+----------------------+----------
      4105 |         6          1 |         7 
           |      0.01       0.00 |      0.00 
-----------+----------------------+----------
      4106 |        11         57 |        68 
           |      0.01       0.11 |      0.05 
-----------+----------------------+----------
      4107 |        15          3 |        18 
           |      0.02       0.01 |      0.01 
-----------+----------------------+----------
      4108 |     1,139         66 |     1,205 
           |      1.22       0.13 |      0.83 
-----------+----------------------+----------
      4110 |     1,666        117 |     1,783 
           |      1.78       0.22 |      1.22 
-----------+----------------------+----------
      4200 |       169          8 |       177 
           |      0.18       0.02 |      0.12 
-----------+----------------------+----------
      4201 |        27          9 |        36 
           |      0.03       0.02 |      0.02 
-----------+----------------------+----------
      4202 |     3,284        258 |     3,542 
           |      3.51       0.49 |      2.43 
-----------+----------------------+----------
      4204 |     2,038     30,855 |    32,893 
           |      2.18      59.00 |     22.56 
-----------+----------------------+----------
      4205 |         1         22 |        23 
           |      0.00       0.04 |      0.02 
-----------+----------------------+----------
      4206 |        73        454 |       527 
           |      0.08       0.87 |      0.36 
-----------+----------------------+----------
      4207 |        62         11 |        73 
           |      0.07       0.02 |      0.05 
-----------+----------------------+----------
      4208 |     3,317        172 |     3,489 
           |      3.55       0.33 |      2.39 
-----------+----------------------+----------
      4209 |       102         83 |       185 
           |      0.11       0.16 |      0.13 
-----------+----------------------+----------
      4210 |       931         83 |     1,014 
           |      1.00       0.16 |      0.70 
-----------+----------------------+----------
      4212 |     3,988        138 |     4,126 
           |      4.27       0.26 |      2.83 
-----------+----------------------+----------
      4213 |     5,204        165 |     5,369 
           |      5.57       0.32 |      3.68 
-----------+----------------------+----------
      4214 |    13,374        313 |    13,687 
           |     14.31       0.60 |      9.39 
-----------+----------------------+----------
      4215 |    24,936      1,814 |    26,750 
           |     26.67       3.47 |     18.35 
-----------+----------------------+----------
      4216 |       529      6,938 |     7,467 
           |      0.57      13.27 |      5.12 
-----------+----------------------+----------
      4217 |       928         43 |       971 
           |      0.99       0.08 |      0.67 
-----------+----------------------+----------
      4220 |     4,872        114 |     4,986 
           |      5.21       0.22 |      3.42 
-----------+----------------------+----------
      4230 |       110          4 |       114 
           |      0.12       0.01 |      0.08 
-----------+----------------------+----------
      4231 |        25          3 |        28 
           |      0.03       0.01 |      0.02 
-----------+----------------------+----------
      4235 |       239         27 |       266 
           |      0.26       0.05 |      0.18 
-----------+----------------------+----------
      4240 |     1,097         34 |     1,131 
           |      1.17       0.07 |      0.78 
-----------+----------------------+----------
      4241 |     3,435         75 |     3,510 
           |      3.67       0.14 |      2.41 
-----------+----------------------+----------
      4242 |     2,225         47 |     2,272 
           |      2.38       0.09 |      1.56 
-----------+----------------------+----------
      4245 |       550         28 |       578 
           |      0.59       0.05 |      0.40 
-----------+----------------------+----------
      4250 |        14          1 |        15 
           |      0.01       0.00 |      0.01 
-----------+----------------------+----------
      4255 |        41          2 |        43 
           |      0.04       0.00 |      0.03 
-----------+----------------------+----------
      4260 |       236         31 |       267 
           |      0.25       0.06 |      0.18 
-----------+----------------------+----------
      4264 |         2          0 |         2 
           |      0.00       0.00 |      0.00 
-----------+----------------------+----------
      4265 |         6          1 |         7 
           |      0.01       0.00 |      0.00 
-----------+----------------------+----------
      4270 |       201          5 |       206 
           |      0.21       0.01 |      0.14 
-----------+----------------------+----------
      4271 |        13          1 |        14 
           |      0.01       0.00 |      0.01 
-----------+----------------------+----------
      4272 |        55          2 |        57 
           |      0.06       0.00 |      0.04 
-----------+----------------------+----------
      4275 |       104          4 |       108 
           |      0.11       0.01 |      0.07 
-----------+----------------------+----------
      4280 |       122          9 |       131 
           |      0.13       0.02 |      0.09 
-----------+----------------------+----------
      4285 |       117          5 |       122 
           |      0.13       0.01 |      0.08 
-----------+----------------------+----------
      4290 |       495         13 |       508 
           |      0.53       0.02 |      0.35 
-----------+----------------------+----------
      4300 |     1,366         38 |     1,404 
           |      1.46       0.07 |      0.96 
-----------+----------------------+----------
      4301 |       503         23 |       526 
           |      0.54       0.04 |      0.36 
-----------+----------------------+----------
      4302 |       612         53 |       665 
           |      0.65       0.10 |      0.46 
-----------+----------------------+----------
      4303 |        52          1 |        53 
           |      0.06       0.00 |      0.04 
-----------+----------------------+----------
      4304 |         9          0 |         9 
           |      0.01       0.00 |      0.01 
-----------+----------------------+----------
      4305 |         5          0 |         5 
           |      0.01       0.00 |      0.00 
-----------+----------------------+----------
      4306 |         3          0 |         3 
           |      0.00       0.00 |      0.00 
-----------+----------------------+----------
      4307 |       135          7 |       142 
           |      0.14       0.01 |      0.10 
-----------+----------------------+----------
      4308 |        39          1 |        40 
           |      0.04       0.00 |      0.03 
-----------+----------------------+----------
      4315 |       197         17 |       214 
           |      0.21       0.03 |      0.15 
-----------+----------------------+----------
      4400 |     2,496      2,492 |     4,988 
           |      2.67       4.77 |      3.42 
-----------+----------------------+----------
      4401 |     3,282      4,297 |     7,579 
           |      3.51       8.22 |      5.20 
-----------+----------------------+----------
      4402 |        23          1 |        24 
           |      0.02       0.00 |      0.02 
-----------+----------------------+----------
      4403 |       521         35 |       556 
           |      0.56       0.07 |      0.38 
-----------+----------------------+----------
      4404 |         7          0 |         7 
           |      0.01       0.00 |      0.00 
-----------+----------------------+----------
      4405 |       116          6 |       122 
           |      0.12       0.01 |      0.08 
-----------+----------------------+----------
      4410 |       111          9 |       120 
           |      0.12       0.02 |      0.08 
-----------+----------------------+----------
      4420 |        78          1 |        79 
           |      0.08       0.00 |      0.05 
-----------+----------------------+----------
      4430 |        91          3 |        94 
           |      0.10       0.01 |      0.06 
-----------+----------------------+----------
      4450 |        52          4 |        56 
           |      0.06       0.01 |      0.04 
-----------+----------------------+----------
      4451 |       698         16 |       714 
           |      0.75       0.03 |      0.49 
-----------+----------------------+----------
      4455 |        43          1 |        44 
           |      0.05       0.00 |      0.03 
-----------+----------------------+----------
      4500 |       123          2 |       125 
           |      0.13       0.00 |      0.09 
-----------+----------------------+----------
      4510 |        41         37 |        78 
           |      0.04       0.07 |      0.05 
-----------+----------------------+----------
      4520 |        40          6 |        46 
           |      0.04       0.01 |      0.03 
-----------+----------------------+----------
      4592 |         3          1 |         4 
           |      0.00       0.00 |      0.00 
-----------+----------------------+----------
      4593 |         1         15 |        16 
           |      0.00       0.03 |      0.01 
-----------+----------------------+----------
      4597 |         1          0 |         1 
           |      0.00       0.00 |      0.00 
-----------+----------------------+----------
     Total |    93,491     52,297 |   145,788 
           |    100.00     100.00 |    100.00 


** SECONDARY DGN

. tab can_dgn2 rec_hcv_stat if inlist(rec_hcv_stat,"P","N"), col

+-------------------+
| Key               |
|-------------------|
|     frequency     |
| column percentage |
+-------------------+

 Secondary |  HCV serology status
 Diagnosis |         N          P |     Total
-----------+----------------------+----------
       999 |     1,543        924 |     2,467 
           |     10.21       5.15 |      7.46 
-----------+----------------------+----------
      4100 |        29          5 |        34 
           |      0.19       0.03 |      0.10 
-----------+----------------------+----------
      4101 |        10          3 |        13 
           |      0.07       0.02 |      0.04 
-----------+----------------------+----------
      4102 |       114         17 |       131 
           |      0.75       0.09 |      0.40 
-----------+----------------------+----------
      4104 |        25        306 |       331 
           |      0.17       1.71 |      1.00 
-----------+----------------------+----------
      4105 |         0          3 |         3 
           |      0.00       0.02 |      0.01 
-----------+----------------------+----------
      4106 |         6         13 |        19 
           |      0.04       0.07 |      0.06 
-----------+----------------------+----------
      4107 |         2          0 |         2 
           |      0.01       0.00 |      0.01 
-----------+----------------------+----------
      4108 |        34         21 |        55 
           |      0.22       0.12 |      0.17 
-----------+----------------------+----------
      4110 |       133          7 |       140 
           |      0.88       0.04 |      0.42 
-----------+----------------------+----------
      4200 |        34         13 |        47 
           |      0.22       0.07 |      0.14 
-----------+----------------------+----------
      4201 |         5          1 |         6 
           |      0.03       0.01 |      0.02 
-----------+----------------------+----------
      4202 |       668        101 |       769 
           |      4.42       0.56 |      2.33 
-----------+----------------------+----------
      4204 |       466      4,850 |     5,316 
           |      3.08      27.04 |     16.08 
-----------+----------------------+----------
      4205 |         8         11 |        19 
           |      0.05       0.06 |      0.06 
-----------+----------------------+----------
      4206 |        18         80 |        98 
           |      0.12       0.45 |      0.30 
-----------+----------------------+----------
      4207 |         5          2 |         7 
           |      0.03       0.01 |      0.02 
-----------+----------------------+----------
      4208 |        39         10 |        49 
           |      0.26       0.06 |      0.15 
-----------+----------------------+----------
      4209 |        16         26 |        42 
           |      0.11       0.14 |      0.13 
-----------+----------------------+----------
      4210 |        96         30 |       126 
           |      0.64       0.17 |      0.38 
-----------+----------------------+----------
      4212 |       634         74 |       708 
           |      4.19       0.41 |      2.14 
-----------+----------------------+----------
      4213 |       394         24 |       418 
           |      2.61       0.13 |      1.26 
-----------+----------------------+----------
      4214 |     2,224        196 |     2,420 
           |     14.71       1.09 |      7.32 
-----------+----------------------+----------
      4215 |     2,279      2,336 |     4,615 
           |     15.07      13.03 |     13.96 
-----------+----------------------+----------
      4216 |        74        827 |       901 
           |      0.49       4.61 |      2.73 
-----------+----------------------+----------
      4217 |        70         21 |        91 
           |      0.46       0.12 |      0.28 
-----------+----------------------+----------
      4220 |       346         33 |       379 
           |      2.29       0.18 |      1.15 
-----------+----------------------+----------
      4230 |        10          2 |        12 
           |      0.07       0.01 |      0.04 
-----------+----------------------+----------
      4231 |         3          0 |         3 
           |      0.02       0.00 |      0.01 
-----------+----------------------+----------
      4235 |        25          3 |        28 
           |      0.17       0.02 |      0.08 
-----------+----------------------+----------
      4240 |        84         11 |        95 
           |      0.56       0.06 |      0.29 
-----------+----------------------+----------
      4241 |       222         10 |       232 
           |      1.47       0.06 |      0.70 
-----------+----------------------+----------
      4242 |       140          8 |       148 
           |      0.93       0.04 |      0.45 
-----------+----------------------+----------
      4245 |        48         12 |        60 
           |      0.32       0.07 |      0.18 
-----------+----------------------+----------
      4250 |         1          0 |         1 
           |      0.01       0.00 |      0.00 
-----------+----------------------+----------
      4255 |         1          0 |         1 
           |      0.01       0.00 |      0.00 
-----------+----------------------+----------
      4260 |        24          2 |        26 
           |      0.16       0.01 |      0.08 
-----------+----------------------+----------
      4264 |         2          1 |         3 
           |      0.01       0.01 |      0.01 
-----------+----------------------+----------
      4265 |         1          1 |         2 
           |      0.01       0.01 |      0.01 
-----------+----------------------+----------
      4270 |         8          1 |         9 
           |      0.05       0.01 |      0.03 
-----------+----------------------+----------
      4272 |         3          0 |         3 
           |      0.02       0.00 |      0.01 
-----------+----------------------+----------
      4275 |         6          1 |         7 
           |      0.04       0.01 |      0.02 
-----------+----------------------+----------
      4280 |        12          0 |        12 
           |      0.08       0.00 |      0.04 
-----------+----------------------+----------
      4285 |         5          1 |         6 
           |      0.03       0.01 |      0.02 
-----------+----------------------+----------
      4290 |        46          7 |        53 
           |      0.30       0.04 |      0.16 
-----------+----------------------+----------
      4300 |       343         60 |       403 
           |      2.27       0.33 |      1.22 
-----------+----------------------+----------
      4301 |        39          5 |        44 
           |      0.26       0.03 |      0.13 
-----------+----------------------+----------
      4302 |       273        124 |       397 
           |      1.81       0.69 |      1.20 
-----------+----------------------+----------
      4303 |         3          0 |         3 
           |      0.02       0.00 |      0.01 
-----------+----------------------+----------
      4305 |         3          0 |         3 
           |      0.02       0.00 |      0.01 
-----------+----------------------+----------
      4306 |         1          1 |         2 
           |      0.01       0.01 |      0.01 
-----------+----------------------+----------
      4307 |         1          1 |         2 
           |      0.01       0.01 |      0.01 
-----------+----------------------+----------
      4315 |        30         12 |        42 
           |      0.20       0.07 |      0.13 
-----------+----------------------+----------
      4400 |     2,415      4,132 |     6,547 
           |     15.97      23.04 |     19.81 
-----------+----------------------+----------
      4401 |     1,860      3,526 |     5,386 
           |     12.30      19.66 |     16.30 
-----------+----------------------+----------
      4402 |         3          1 |         4 
           |      0.02       0.01 |      0.01 
-----------+----------------------+----------
      4403 |        89         14 |       103 
           |      0.59       0.08 |      0.31 
-----------+----------------------+----------
      4404 |         2          3 |         5 
           |      0.01       0.02 |      0.02 
-----------+----------------------+----------
      4405 |         4          3 |         7 
           |      0.03       0.02 |      0.02 
-----------+----------------------+----------
      4410 |        11          8 |        19 
           |      0.07       0.04 |      0.06 
-----------+----------------------+----------
      4420 |        26          3 |        29 
           |      0.17       0.02 |      0.09 
-----------+----------------------+----------
      4430 |         9         14 |        23 
           |      0.06       0.08 |      0.07 
-----------+----------------------+----------
      4450 |         8          2 |        10 
           |      0.05       0.01 |      0.03 
-----------+----------------------+----------
      4451 |         7          9 |        16 
           |      0.05       0.05 |      0.05 
-----------+----------------------+----------
      4455 |        11          4 |        15 
           |      0.07       0.02 |      0.05 
-----------+----------------------+----------
      4500 |         4          1 |         5 
           |      0.03       0.01 |      0.02 
-----------+----------------------+----------
      4510 |         2          1 |         3 
           |      0.01       0.01 |      0.01 
-----------+----------------------+----------
      4520 |         6          3 |         9 
           |      0.04       0.02 |      0.03 
-----------+----------------------+----------
      4598 |        55         13 |        68 
           |      0.36       0.07 |      0.21 
-----------+----------------------+----------
     Total |    15,118     17,934 |    33,052 
           |    100.00     100.00 |    100.00 
*/



/*

*For Kidneys, primary diagnosis:

tab can_dgn rec_hcv_stat_ki if inlist(rec_hcv_stat_ki,"P","N"), col

   Primary |  HCV serology status
 Diagnosis |         N          P |     Total
-----------+----------------------+----------
       999 |    41,667      2,794 |    44,461 
           |     10.94      13.18 |     11.06 
-----------+----------------------+----------
      3000 |     1,675         88 |     1,763 
           |      0.44       0.42 |      0.44 
-----------+----------------------+----------
      3001 |     6,312        469 |     6,781 
           |      1.66       2.21 |      1.69 
-----------+----------------------+----------
      3002 |       888         47 |       935 
           |      0.23       0.22 |      0.23 
-----------+----------------------+----------
      3003 |       166         13 |       179 
           |      0.04       0.06 |      0.04 
-----------+----------------------+----------
      3004 |    20,706        382 |    21,088 
           |      5.44       1.80 |      5.24 
-----------+----------------------+----------
      3005 |       504         16 |       520 
           |      0.13       0.08 |      0.13 
-----------+----------------------+----------
      3006 |    25,151      1,044 |    26,195 
           |      6.60       4.93 |      6.51 
-----------+----------------------+----------
      3007 |     4,789        181 |     4,970 
           |      1.26       0.85 |      1.24 
-----------+----------------------+----------
      3008 |    36,531        884 |    37,415 
           |      9.59       4.17 |      9.30 
-----------+----------------------+----------
      3009 |     3,749        196 |     3,945 
           |      0.98       0.92 |      0.98 
-----------+----------------------+----------
      3010 |       251         11 |       262 
           |      0.07       0.05 |      0.07 
-----------+----------------------+----------
      3011 |    10,034        515 |    10,549 
           |      2.63       2.43 |      2.62 
-----------+----------------------+----------
      3012 |     6,788        413 |     7,201 
           |      1.78       1.95 |      1.79 
-----------+----------------------+----------
      3013 |       410         15 |       425 
           |      0.11       0.07 |      0.11 
-----------+----------------------+----------
      3014 |       194          5 |       199 
           |      0.05       0.02 |      0.05 
-----------+----------------------+----------
      3015 |       312          8 |       320 
           |      0.08       0.04 |      0.08 
-----------+----------------------+----------
      3016 |       766         29 |       795 
           |      0.20       0.14 |      0.20 
-----------+----------------------+----------
      3017 |       117          9 |       126 
           |      0.03       0.04 |      0.03 
-----------+----------------------+----------
      3018 |    12,722        323 |    13,045 
           |      3.34       1.52 |      3.24 
-----------+----------------------+----------
      3019 |        71          1 |        72 
           |      0.02       0.00 |      0.02 
-----------+----------------------+----------
      3020 |       151          4 |       155 
           |      0.04       0.02 |      0.04 
-----------+----------------------+----------
      3021 |       893         41 |       934 
           |      0.23       0.19 |      0.23 
-----------+----------------------+----------
      3022 |        29          3 |        32 
           |      0.01       0.01 |      0.01 
-----------+----------------------+----------
      3023 |       245          6 |       251 
           |      0.06       0.03 |      0.06 
-----------+----------------------+----------
      3024 |       964         42 |     1,006 
           |      0.25       0.20 |      0.25 
-----------+----------------------+----------
      3025 |     1,656         64 |     1,720 
           |      0.43       0.30 |      0.43 
-----------+----------------------+----------
      3026 |       135          7 |       142 
           |      0.04       0.03 |      0.04 
-----------+----------------------+----------
      3027 |       808        106 |       914 
           |      0.21       0.50 |      0.23 
-----------+----------------------+----------
      3028 |       741         25 |       766 
           |      0.19       0.12 |      0.19 
-----------+----------------------+----------
      3029 |       287         16 |       303 
           |      0.08       0.08 |      0.08 
-----------+----------------------+----------
      3030 |     1,745         71 |     1,816 
           |      0.46       0.34 |      0.45 
-----------+----------------------+----------
      3031 |     3,573        119 |     3,692 
           |      0.94       0.56 |      0.92 
-----------+----------------------+----------
      3032 |       515         13 |       528 
           |      0.14       0.06 |      0.13 
-----------+----------------------+----------
      3033 |     1,035         26 |     1,061 
           |      0.27       0.12 |      0.26 
-----------+----------------------+----------
      3034 |     9,181        844 |    10,025 
           |      2.41       3.98 |      2.49 
-----------+----------------------+----------
      3035 |       486         22 |       508 
           |      0.13       0.10 |      0.13 
-----------+----------------------+----------
      3036 |       256          9 |       265 
           |      0.07       0.04 |      0.07 
-----------+----------------------+----------
      3038 |       369         28 |       397 
           |      0.10       0.13 |      0.10 
-----------+----------------------+----------
      3039 |     9,087        521 |     9,608 
           |      2.39       2.46 |      2.39 
-----------+----------------------+----------
      3040 |    70,708      5,011 |    75,719 
           |     18.56      23.65 |     18.83 
-----------+----------------------+----------
      3041 |    15,246        788 |    16,034 
           |      4.00       3.72 |      3.99 
-----------+----------------------+----------
      3042 |     1,548         53 |     1,601 
           |      0.41       0.25 |      0.40 
-----------+----------------------+----------
      3043 |     2,091        122 |     2,213 
           |      0.55       0.58 |      0.55 
-----------+----------------------+----------
      3044 |       977         34 |     1,011 
           |      0.26       0.16 |      0.25 
-----------+----------------------+----------
      3045 |       104          0 |       104 
           |      0.03       0.00 |      0.03 
-----------+----------------------+----------
      3046 |       117         10 |       127 
           |      0.03       0.05 |      0.03 
-----------+----------------------+----------
      3047 |       349         17 |       366 
           |      0.09       0.08 |      0.09 
-----------+----------------------+----------
      3048 |     2,458        464 |     2,922 
           |      0.65       2.19 |      0.73 
-----------+----------------------+----------
      3049 |        63         39 |       102 
           |      0.02       0.18 |      0.03 
-----------+----------------------+----------
      3050 |       176         14 |       190 
           |      0.05       0.07 |      0.05 
-----------+----------------------+----------
      3051 |       986         78 |     1,064 
           |      0.26       0.37 |      0.26 
-----------+----------------------+----------
      3052 |     3,073         99 |     3,172 
           |      0.81       0.47 |      0.79 
-----------+----------------------+----------
      3053 |       361          7 |       368 
           |      0.09       0.03 |      0.09 
-----------+----------------------+----------
      3054 |     2,117         51 |     2,168 
           |      0.56       0.24 |      0.54 
-----------+----------------------+----------
      3055 |       246         11 |       257 
           |      0.06       0.05 |      0.06 
-----------+----------------------+----------
      3056 |        48          0 |        48 
           |      0.01       0.00 |      0.01 
-----------+----------------------+----------
      3057 |       359         12 |       371 
           |      0.09       0.06 |      0.09 
-----------+----------------------+----------
      3058 |        31          2 |        33 
           |      0.01       0.01 |      0.01 
-----------+----------------------+----------
      3059 |       961         41 |     1,002 
           |      0.25       0.19 |      0.25 
-----------+----------------------+----------
      3060 |        74          3 |        77 
           |      0.02       0.01 |      0.02 
-----------+----------------------+----------
      3061 |         4          0 |         4 
           |      0.00       0.00 |      0.00 
-----------+----------------------+----------
      3062 |        19          0 |        19 
           |      0.00       0.00 |      0.00 
-----------+----------------------+----------
      3063 |       842         60 |       902 
           |      0.22       0.28 |      0.22 
-----------+----------------------+----------
      3064 |       194          1 |       195 
           |      0.05       0.00 |      0.05 
-----------+----------------------+----------
      3066 |        37          2 |        39 
           |      0.01       0.01 |      0.01 
-----------+----------------------+----------
      3068 |       417         15 |       432 
           |      0.11       0.07 |      0.11 
-----------+----------------------+----------
      3069 |     9,041        368 |     9,409 
           |      2.37       1.74 |      2.34 
-----------+----------------------+----------
      3070 |    57,660      3,790 |    61,450 
           |     15.14      17.88 |     15.28 
-----------+----------------------+----------
      3071 |     1,138         77 |     1,215 
           |      0.30       0.36 |      0.30 
-----------+----------------------+----------
      3072 |     2,247        518 |     2,765 
           |      0.59       2.44 |      0.69 
-----------+----------------------+----------
      3073 |       681         16 |       697 
           |      0.18       0.08 |      0.17 
-----------+----------------------+----------
      3074 |       561         76 |       637 
           |      0.15       0.36 |      0.16 
-----------+----------------------+----------
      5001 |        17          2 |        19 
           |      0.00       0.01 |      0.00 
-----------+----------------------+----------
      5002 |         1          0 |         1 
           |      0.00       0.00 |      0.00 
-----------+----------------------+----------
     Total |   380,911     21,191 |   402,102 
           |    100.00     100.00 |    100.00 


*/

cap log close
